import util from '@/libs/util';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                align: 'center',
                width: 60,
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
                title: '机构名称',
                key: 'organizename'
            }, {
                title: '创建时间',
                key: 'createTime'
            }, {
                title: '级别',
                key: 'level'
            }, {
                title: '操作',
                key: 'action',
                width: 300,
                align: 'center',
                render: (h, params) => {
                    let isShow = 'none';
                    if (params.row.releases == 'NO') {
                        isShow = 'inline';
                    }  return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.detail(params.row);
                                }
                            }
                        }, '详情'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            data: [],
            formData: {},
            viewModalVisible: false,
            saveModalVisible: false,
            priceModalVisible: false,

            total: 0,
            pageIndex: 1,
            pageSize: 10,
            queryCon: {
                createTime: '',
                level: '',
                organize: '',
                themesun: '',
                diplomasun: ''
            },
            organizeList: [],
            id: ''
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/level/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                organizename: this.organizename,
                orgId: eval('(' + Cookies.get('baseUser') + ')').orgId
            })).then(res => {
                var lists = res.data.data.pageInfo.list;
                for (var i = 0; i < lists.length; i++) {
                    if (lists[i].organize) {
                        lists[i]['organizename'] = lists[i].organize.orgName;
                    }
                    lists[i]['createTime'] = this.getDate(lists[i].createTime);
                }
                this.organizeList = res.data.data.organizeList;
                this.data = res.data.data.pageInfo.list;
                this.total = res.data.data.pageInfo.total;
            });
        },
        detail (obj) {
            let _this = this;
            util.ajax.post('/busine/level/detail', util.toFormData({
                id: obj.id
            })).then(res => {
                if (res.data.data) {
                    var li = res.data.data;
                    if (li.organize) {
                        li ["organizename"] = obj.organize.orgName;
                    }
                    li['createTime'] = this.getDate(li.createTime);
                    _this.formData = res.data.data;
                    _this.viewModalVisible = true;
                } else {
                    _this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        showAdd () {
            this.formData = {};
            this.saveModalVisible = true;
        },
        showEdit (obj) {
            this.formData = {};
            util.ajax.post('/busine/level/detail', util.toFormData({
                id: obj.id
            })).then(res => {
                if (res.data.data) {
                    var li = res.data.data;
                    if (li.organize) {
                        li["orgId"] = li.organize.id;
                    }
                    li['createTime'] = this.getDate(li.createTime);

                    this.formData = res.data.data;
                    this.saveModalVisible = true;
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        activitySave () {
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            let baseuser = eval('(' + Cookies.get('baseUser') + ')');
            if (baseuser.orgId != "") {
                formData['orgId'] = baseuser.orgId;
            }
            util.ajax.post('/busine/level/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.saveModalVisible = false;
                    this.$Message.success('保存成功！');
                    this.pageIndex = 1;
                    this.loadList();
                } else {
                    this.$Message.warning(res.data.message || '未知错误！');
                }
            });
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/busine/level/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        getDate (data) {
            let myDate;

            if (data instanceof Date) {
                myDate = data;
            } else if (typeof (data) == 'number') {
                myDate = new Date(data);
            } else {
                return data;
            }

            let Y = myDate.getFullYear(),
                M = myDate.getMonth() + 1,
                D = myDate.getDate(),
                H = myDate.getHours(),
                Min = myDate.getMinutes(),
                S = myDate.getSeconds();

            if (M < 10) {
                M = '0' + M;
            }
            if (D < 10) {
                D = '0' + D;
            }
            if (H < 10) {
                H = '0' + H;
            }
            if (Min < 10) {
                Min = '0' + Min;
            }
            if (S < 10) {
                S = '0' + S;
            }
            return Y + '-' + M + '-' + D + '  ' + H + ':' + Min + ':' + S;
        },
        refresh () {
            this.queryCon = {
                createTime: '',
                level: '',
                organize: '',
                themesun: '',
                diplomasun: ''
            };
            this.loadList();
        }
    }
};